package com.hucc.huccgps;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBHelper extends SQLiteOpenHelper {

	public static final String TABLE_RELATIVES_CONTENT = "RelativesContent";
	public static final int VERSION = 1;
	public static final String dbName = "SosDB";
	public static Context mContext = null;
	public SQLiteDatabase db = null;
	
	public DBHelper(Context paramContext) {
		super(paramContext, "SosDB", null, 1);
		mContext = paramContext;
	}

	@Override
	public void onCreate(SQLiteDatabase paramSQLiteDatabase) {
		paramSQLiteDatabase.execSQL("create table RelativesContent(id integer primary key autoincrement,name text , number text)");
		closeDatabase();
	}

	private void closeDatabase() {
		if (this.db == null) 
			return;
		this.db.close();
	}

	@Override
	public void onUpgrade(SQLiteDatabase paramSQLiteDatabase, int paramInt1, int paramInt2) {
		onCreate(paramSQLiteDatabase);
	}

	public ArrayList<Item> getRelativesList() {
		String idf = null;
		String namef = null;
		String numf = null;
		ArrayList localArrayList = new ArrayList();
		openDatabase();
		Cursor localCursor = this.db.query("RelativesContent", null, null, null, null, null, null);
		if(localCursor == null)
			return localArrayList;
	    while (localCursor.moveToNext()) {
			Item localItem = new Item();
			idf = localCursor.getString(localCursor.getColumnIndex("id"));
			namef = localCursor.getString(localCursor.getColumnIndex("name"));
			numf = localCursor.getString(localCursor.getColumnIndex("number"));
			localItem.setId(idf);
			Log.d("JGPS/MainActivity", "dbid=" + idf);
			if ((namef == null) || ("".equals(namef))) {
				localItem.setName("");
			}else {
				localItem.setName(namef);
			}
			if ((numf == null) || ("".equals(numf))) {
				localItem.setNumber("");
			}else {
				localItem.setNumber(numf);
			}
			localArrayList.add(localItem);
		}		
		return localArrayList;
	}

	private void openDatabase() {
		this.db = super.getWritableDatabase();
	}

	public boolean addRelativesItem(Item localItem) {
		openDatabase();
		ContentValues localContentValues = new ContentValues();
		localContentValues.put("name", localItem.getName());
		localContentValues.put("number", localItem.getNumber());
		long l = this.db.insert("RelativesContent", null, localContentValues);
		closeDatabase();
		return (1<=0L);
	}

	public boolean updateRelativesItem(Item paramItem) {
		openDatabase();
		ContentValues localContentValues = new ContentValues();
		localContentValues.put("name", paramItem.getName());
		localContentValues.put("number", paramItem.getNumber());
		SQLiteDatabase localSQLiteDatabase = this.db;
		String[] arrayOfString = new String[1];
		arrayOfString[0] = paramItem.getId();
		long l = localSQLiteDatabase.update("RelativesContent", localContentValues, "id=?", arrayOfString);
	    closeDatabase();
	    return  (1 <= 0L);
	}

}
